package com.example.zheng.qq.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zheng.qq.R;
import com.example.zheng.qq.controller.CreateTable;

/**
 * Created by zheng on 17-4-26.
 */
public class RegisterLayout extends Activity{
    private CreateTable mCreateTable;
    private EditText mName;
    private EditText mPass;
    private EditText mPass1;
    private SQLiteDatabase mDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        mName=(EditText)findViewById(R.id.name);
        mPass=(EditText)findViewById(R.id.pass);
        mPass1=(EditText)findViewById(R.id.repass);
        mCreateTable=new CreateTable(this,"USER.db",null,1);
        mCreateTable.getWritableDatabase();
        mDb=mCreateTable.getWritableDatabase();



    }

    /**
     * 将输入格式正确的用户信息存放在数据库中
     * @param v
     */
    public void doclick(View v){
        switch (v.getId()){
            case R.id.register2:
                String name=mName.getText().toString();
                String pass=mPass.getText().toString();
                String pass1=mPass1.getText().toString();
                Cursor data=mDb.rawQuery("select * from user where uname=?",new String[]{name});
                if(!data.moveToNext()) {
                    if (!"".equals(name) && !"".equals(pass) && pass.equals(pass1)) {
                        mDb.execSQL("insert into user(uname,pass,picture) values(?,?,?)", new String[]{name, pass, "android.resource://com.example.zheng.qq/drawable/qq"});
                        finish();
                    }

                }else{
                    Toast.makeText(this,"该用户已经注册过",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.back:
                finish();
//                overridePendingTransition(R.anim.pull_in_left,R.anim.pull_out_right);
        }
    }


}
